#include "slogin.h"
#include "ui_slogin.h"
#include <QWidget>
#include "admininfo.h"
#include "teacherinfo.h"
#include "studentinfo.h"
#include <QDebug>
#include <QPainter>
#include <QMovie>
#include <QHBoxLayout>
#include <QCursor>
#include <QMessageBox>
#include <QString>
#include <QSqlQuery>
#include <QSqlRecord>
#include"checkininfo.h"
#include"formsclass.h"
#include<QProcess>


SLogin::SLogin(QWidget *parent) :
    QWidget(parent),ui(new Ui::SLogin)
{
    ui->setupUi(this);
    initWindow();
}

void SLogin::insert()
  {
      CheckinInfo infoc;
      ExecSQL ex;
      qDebug()<<m_xid<<m_ScienceID<<m_teacherId <<m_Classid<<m_RoomID<<
              m_CheckinTime<<m_xuehao;
      infoc = ex.insertCheckinInfo(m_xid,m_ScienceID,m_teacherId ,m_Classid,m_RoomID,
                                                m_CheckinTime,m_xuehao);
      infoc.updateCheckinInfo(m_xid,m_ScienceID,m_teacherId ,m_Classid,m_RoomID,
                              m_CheckinTime,m_xuehao);
      qDebug()<<infoc.getCheckinTime();
      qDebug()<<"m_xid ="<<m_xid;
      infoc.display();
  }

SLogin::~SLogin()
{
    delete ui;
}
void SLogin::showsloginW()
{
    emit siignalslogin();
    this->show();
}

void SLogin::initWindow()
{
    ui->comboBox_bianhao->setEditable(true);
    QLineEdit* lineEdit = ui->comboBox_bianhao->lineEdit();
    lineEdit->setPlaceholderText(tr("编号"));
    ui->lineEdit_pwd->setPlaceholderText(tr("密码"));
}

void SLogin::slotcheckinW()
{
    checkinW->show();
    checkinW->str = str;
    checkinW->m_CheckinTime =m_CheckinTime;
    checkinW->m_ScienceID =m_ScienceID;
    checkinW->m_xid =m_xid;
    checkinW->m_Classid =m_Classid;
    checkinW->m_RoomID =m_RoomID;
    checkinW->m_teacherId =m_teacherId;
}

void SLogin::on_pb_stulongin_clicked()
{
    QString id = ui->comboBox_bianhao->currentText();
    ExecSQL exes;
    infoS = exes.selectStudentInfo(id);
    m_xuehao = infoS.getxuehao();
    qDebug()<<"m_xuehao =="<<m_xuehao;
//    QString m_xid;
//    QString m_ScienceID;
//    QString m_teacherId;
//    QString m_Classid;
//    QString m_RoomID;
//    QString m_CheckinTime;
     qDebug()<<"m_xid =="<<m_xid;
    table_pwd = infoS.getpswd();
    actor = 3;
    infoS.display();
}

void SLogin::on_pb_facelongin_clicked()
{
//    ui->lineEdit_pwd->hide();
    pronew.startDetached("D:\\openmv\\bin\\openmvide.exe",QStringList());
    if(pronew.isOpen() == true)
    {}
    else
    {
          pronew.close();
    }

    active = 0;
    qDebug()<<"0";
}

void SLogin::on_pb_pwdlogin_clicked()
{
//    ui->textEdit_face->hide();
     active = 1;
     qDebug()<<"1";
}

void SLogin::on_pb_login_clicked()
{
    if(active == 1)
    {
        QString pwd = ui->lineEdit_pwd->text();
        if(pwd == table_pwd)
        {
            switch(actor)
            {
            case 1:
               break;
            case 2:
              break;
            case 3:
                //学生签到
              insert();
              ui->textEdit_face->setPlaceholderText(tr("插入成功"));
                break;
           default:
                break;
            }
            qDebug()<<"success: insert people!!!";
        }else{
            QMessageBox msgBox;
            msgBox.setText("Error: Your ID or Password !!");
            msgBox.setInformativeText("Please try again !");
            msgBox.setStandardButtons(QMessageBox::Retry | QMessageBox::Cancel);
            msgBox.setDefaultButton(QMessageBox::Retry);

            int ret = msgBox.exec();
            switch (ret) {
              case QMessageBox::Retry:
                  ui->comboBox_bianhao->setFocus();
                  break;
              case QMessageBox::Cancel:
                  this->close();
                  break;
              default:
                  // should never be reached
                  break;
            qDebug()<<"fail : find people!!";
            }
        }
    }else if(active == 0)
    {
//人脸识别
                if(pronew.isOpen() == false)
                {
                    pronew.close();
                    switch(actor)
                    {
                    case 1:
                        break;
                    case 2:
                         break;
                    case 3:
                        insert();
                        ui->textEdit_face->setPlaceholderText(tr("插入成功"));
                         break;
                   default:
                        break;
                    }
//          if(pronew.exitStatus() == 0)
//          {
//              pronew.close();
//           QMessageBox msgBox;
//              msgBox.setText("Error: Your face !!");
//              msgBox.setInformativeText("Please try again !");
//              msgBox.setStandardButtons(QMessageBox::Retry | QMessageBox::Cancel);
//            msgBox.setDefaultButton(QMessageBox::Retry);

//            int ret = msgBox.exec();
//                  switch (ret) {
//                    case QMessageBox::Retry:
//                    ui->textEdit_face->setFocus();
//                      break;
//                    case QMessageBox::Cancel:
//                      this->close();
//                         break;
//                        default:
//                         // should never be reached
//                           break;
//                  qDebug()<<"fail : find people!!";

//                    }
//          }else{
//              pronew.close();
//          }
        }
}
}

void SLogin::on_pb_forgetpwd_clicked()
{
   checkinW = new ChechinTable;
    connect(checkinW,&ChechinTable::signalscheckinW,this,
            &SLogin::slotcheckinW);
    this->hide();
    checkinW->showchechinW();
    checkinW->selectBoxSlot(m_xid,m_ScienceID,m_teacherId ,m_Classid,m_RoomID,
                                                                                          m_CheckinTime,m_xuehao);
}


